import API from '../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {

    commentCon:'',
    footerIslike: false,
    zancount: 0,
    allId:0,
    isFocus:false,
    level:1,
    questionid:0,
    from:1
  },
   /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (options.allId) {
      this.setData({
        allId:options.allId,
        questionid:options.questionid,
        from:options.from
      })
      if (options.from=='2') {
        this.getAllReplyData(options.allId)
      } else {
        this.getCircleAllReplyData(options.allId)
      }
     
    }
  },
  // 获取圈子查看全部回复数据
  getCircleAllReplyData (id) {
    API.lookAllReplyCircle({reviewid:id}).then(res=>{
      console.log(res.data.prequestion)
      if (res.ret == 0) {
        this.setData({
          prequestion:res.data.prequestion,
          sonquestion:res.data.sonquestion
        })
      }
    })
  },
  footerLike(e) {
    var footerIslike = this.data.footerIslike
    var likeNum = e.currentTarget.dataset.zancount
    var status = 0
    if (footerIslike) {
      likeNum--
      status = 0
    } else {
      likeNum++
      status = 1
    }
    footerIslike = !footerIslike
    var data = {
      questionid: this.data.allId,
      status: status
    }
    API.like(data).then(res => {
      if (res.ret == 0) {
        this.getAllReplyData(this.data.allId)
      }
    })

    this.setData({
      footerIslike: footerIslike,
      zancount: likeNum
    })
  },
    // 回复
    reply (e) {
      var dataset = e.currentTarget.dataset
      console.log(dataset)
      this.setData({
        commentCon:'回复@'+dataset.nickname+':',
        level: 1,
        parentid:dataset.id,
        isFocus:true
      })
  
    },
    commitReply(e) {
      var dataset = e.currentTarget.dataset
      console.log(dataset)
      this.setData({
        commentCon:'回复@'+dataset.nickname+':',
        level: 2,
        parentid:dataset.id,
        isFocus:true
      })
  
    },
  getfooterComment (e) {
    this.setData({
      commentCon:e.detail.value
    })
    
  },
  footerComment(e) {
    console.log(e.detail.value)
    var data = {
      questionid: this.data.questionid,
      content: this.data.commentCon,
      parentid: this.data.allId,
      level: this.data.level
    }
    API.comment(data).then(res => {
      console.log(res)
      if (res.ret == 0) {
        wx.showToast({
          title: '评论成功',
          icon: 'success',
          duration: 2000
        })
       this.getAllReplyData (this.data.allId)
        this.setData({
          commentCon:'',
          level: 1
        })
      }
    })
  },
 
  getAllReplyData (id) {
    API.allReply({reviewid:id}).then(res=>{
      console.log(res.data.prequestion)
      if (res.ret == 0) {
        this.setData({
          prequestion:res.data.prequestion,
          sonquestion:res.data.sonquestion
        })
      }
    })
  },
  // 来自于圈子
  footerCircleComment() {
    if (this.data.commentCon == '') {
      wx.showModal({
        title: '',
        content: '请输入评论'
      })
      return
    }
    var data = {
      content: this.data.commentCon,
      pid: this.data.allId,
      caid: this.data.questionid,
      level: this.data.level
    }
    API.addquanzireview(data).then(res => {
      if (res.ret == 0) {
        wx.showToast({
          title: '评论成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(() => {
          this.getCircleAllReplyData(this.data.allId)
        }, 2000)
        this.setData({
          commentCon: '',
          isFocus: false
        })
      }
    })
  },
  onShow: function () {

  }
})